package com.linkvin.ec.hcs.controller;

import com.google.common.collect.Lists;
import com.linkvin.ec.hcs.dao.model.mdm.VSeries;
import com.linkvin.ec.hcs.dao.model.mdm.bo.VModelBo;
import com.linkvin.ec.hcs.service.mdm.VSeriesService;
import com.linkvin.ec.hcs.vo.RestVO;
import com.linkvin.ec.hcs.vo.VModelTreeVO;
import com.linkvin.ec.hcs.vo.VModelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/series", produces = MediaType.APPLICATION_JSON_VALUE)
public class VSeriesController {


    @Autowired
    private VSeriesService vSeriesService;

    private static final Logger LOGGER = LoggerFactory.getLogger(VSeriesController.class);

    @GetMapping(value = "/treeChildren")
    public RestVO<List<VModelTreeVO>> getSeriesTree(@RequestParam("id") Integer id){
        RestVO<List<VModelTreeVO>> result = new RestVO<>(0,"success");
        LOGGER.info("VSeriesController.getSeriesTree params : {}",id);
        try{
            List<VModelTreeVO> vModelTreeVOList = new ArrayList<>();
            List<VSeries> vSerieses = vSeriesService.listAllSeriesByBrandId(id);
            vSerieses.forEach(vSeries -> {
                VModelTreeVO vModelTreeVO = new VModelTreeVO();
                vModelTreeVO.setId(vSeries.getId());
                vModelTreeVO.setLabel(vSeries.getSeriesName());
                vModelTreeVO.setMode("series");
                vModelTreeVO.setIsleaf(true);
                vModelTreeVOList.add(vModelTreeVO);
            });
            result.setData(vModelTreeVOList);

        }catch (Exception ex){
            LOGGER.info("VSeriesController.getSeriesTree ERROR Message : {} ", ex.getMessage());
            result.setCode(30005);
            result.setMessage("车型数据读取失败");
        }
        return result;
    }

    @PostMapping(value = "/create")
    public RestVO<VModelVO> create(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VSeriesController.create params : {}", vModelBo);
        try {

            VModelVO vModelVO =new VModelVO();
            vModelBo = vSeriesService.addSeries(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VSeriesController.create ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/update")
    public RestVO<VModelVO> update(@RequestBody VModelBo vModelBo){
        RestVO<VModelVO> result = new RestVO<>(0,"success");
        LOGGER.info("VSeriesController.update params : {}", vModelBo);
        try {

            VModelVO vModelVO =new VModelVO();
            vModelBo = vSeriesService.updateSeries(vModelBo);
            BeanUtils.copyProperties(vModelBo,vModelVO);
            result.setData(vModelVO);
        }catch (Exception e){
            LOGGER.error("VSeriesController.update ERROR Message : {} ", e.getMessage());
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/delete")
    public RestVO<Boolean> delete(@RequestBody List<Integer> ids){

        RestVO<Boolean> result = new RestVO<>(0,"success");
        LOGGER.info("VSeriesController.delete params : {}", ids);

        try {
            int i =  vSeriesService.deleteSeries(ids);
            Boolean blnrtn = i > 0;
            result.setData(blnrtn);
        }catch (Exception e){
            LOGGER.error("VSeriesController.delete ERROR Message : {} ", e.getMessage());
            result.setData(false);
            result.setCode(30004);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    @GetMapping(value = "/all")
    public RestVO<List<VModelVO>> getAllSeries(@RequestParam("id")  Integer seriesId){
        RestVO<List<VModelVO>> result = new RestVO<>(0,"success");
        LOGGER.info("VSeriesController.getAllSeries params:{} ",seriesId);
        List<VSeries> vSerieses = vSeriesService.listAllSeriesBySeriesId(seriesId);
        List<VModelVO> listVModelVo = Lists.newArrayList();
        vSerieses.forEach(vSeries -> {
            VModelVO vModelVO = new VModelVO();
            vModelVO.setSeriesId(vSeries.getId());
            vModelVO.setSeriesName(vSeries.getSeriesName());
            listVModelVo.add(vModelVO);
        });
        result.setData(listVModelVo);
        return result;
    }

}
